#include "editcat.h"
#include "ui_editcat.h"
#include<QMessageBox>
#include<QDesktopWidget>
#include<QDebug>
//This class represents a list of existing categories to edit or delete it

EditCat::EditCat(QString cat,QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::EditCat)
{
    ui->setupUi(this);
    catnm=cat;
    QRect innersize=QApplication::desktop()->availableGeometry(this);
    //ui->listWidget->setGeometry(0,50,(innersize.width()),(innersize.height()));
    ui->scrollArea->setGeometry(0,0,(innersize.width()),(innersize.height()));
    ui->listWidget->setEditFocus(true);
    QAction* backAction = new QAction( tr("Back"), this );
     backAction->setSoftKeyRole( QAction::NegativeSoftKey );
     connect(backAction, SIGNAL(triggered()), this, SLOT(back()));
     addAction( backAction );
     int fontSize=10;
     ui->label->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));

     item1=new QListWidgetItem("Edit",ui->listWidget,QListWidgetItem::UserType);
     item1->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));

     item2=new QListWidgetItem("Delete",ui->listWidget,QListWidgetItem::UserType);
     item2->setFont(QFont( "lucida", fontSize, QFont::Normal, FALSE ));
}

EditCat::~EditCat()
{
    delete ui;
}

void EditCat::on_listWidget_itemActivated(QListWidgetItem* item)
{
    if(item==item1)
    {
        (new AddCategory(catnm))->show();
    }
    if(item==item2)
    {
        QMessageBox msg;
       // msg.setText("It will delete category permanently");
        msg.setInformativeText("Do you really want to delete ?");
        msg.setStandardButtons(QMessageBox::Yes|QMessageBox::No);
        msg.setDefaultButton(QMessageBox::Yes);
        int res=msg.exec();
        if(res==QMessageBox::Yes)
        {
            if(db.db_connect())
            {
                if(db.db_category_delete(catnm))
                {
                    QMessageBox::information(0,"Success !","Category deleted successfully !");
                    (new CatSelect)->show();
                }
                else
                    QMessageBox::information(0,"Error !","Can not delete category !");
            db.db_close();
            }
        }
    }

}
void EditCat::back()
{
    (new CatSelect)->show();
}

